<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
use Illuminate\Database\Eloquent\SoftDeletingTrait;

class User extends Eloquent implements UserInterface, RemindableInterface {

	use UserTrait, RemindableTrait, SoftDeletingTrait;

	/**
	 * The database table used by the model.
	 *
	 * @var string
	 */
	protected $table = 'users';

	/**
	 * The attributes excluded from the model's JSON form.
	 *
	 * @var array
	 */
	protected $hidden = array('password', 'remember_token');

	protected $dates = ['deleted_at'];

	protected $primaryKey = 'pk';

	public function setPasswordAttribute($pw)
	{
		$this->attributes['password'] = Hash::make($pw);
	}

	public function userType()
	{
		return $this->belongsTo('UserType', 'user_type_fk', 'pk');
	}

	public function items()
	{
		return $this->hasMany('Item', 'created_by_fk', 'pk');
	}

	public function createdRequests()
	{
		return $this->hasMany('ItemRequest', 'requested_by_fk', 'pk');
	}

	public function approvedRequests()
	{
		return $this->hasMany('ItemRequest', 'approved_by_fk', 'pk');
	}

	public function getFullName()
	{
		return $this->name.($this->first_lastname != "" ? " ".$this->first_lastname : "").($this->second_lastname != "" ? " ".$this->second_lastname : "");
	}
}

Validator::extend('olderThan', function($attribute, $value, $parameters)
{
    $minAge = ( ! empty($parameters)) ? (int) $parameters[0] : 18;
    return (new DateTime)->diff(new DateTime($value))->y >= $minAge;

    // or the same using Carbon:
    // return Carbon\Carbon::now()->diff(new Carbon\Carbon($value))->y >= $minAge;
});
?>